package com.itmk.web.room_category.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.room_category.entity.CategoryPage;
import com.itmk.web.room_category.entity.RoomCategory;
import com.itmk.web.room_category.service.RoomCategoryService;
import com.itmk.web.sys_major.entity.Major;
import com.itmk.web.sys_major.entity.SelectMajor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author java实战基地
 * @since 2023-10-10
 */
@RestController
@RequestMapping("/api/roomCategory")
public class RoomCategoryController {
    @Autowired
    private RoomCategoryService roomCategoryService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody RoomCategory roomCategory){
        if(roomCategoryService.save(roomCategory)){
            return ResultUtils.success("新增成功");
        }
        return ResultUtils.error("新增失败!");
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody RoomCategory roomCategory){
        if(roomCategoryService.updateById(roomCategory)){
            return ResultUtils.success("编辑成功");
        }
        return ResultUtils.error("编辑失败!");
    }

     //删除
    @DeleteMapping("/{categoryId}")
    public ResultVo delete(@PathVariable("categoryId") Integer categoryId){
        if(roomCategoryService.removeById(categoryId)){
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public ResultVo list(CategoryPage parm){
        //构造分页对象
        IPage<RoomCategory> page = new Page(parm.getCurrentPage(),parm.getPageSize());
        //构造查询条件
        QueryWrapper<RoomCategory> query = new QueryWrapper<>();
        query.lambda().like(StringUtils.isNotEmpty(parm.getCategoryName()),RoomCategory::getCategoryName,parm.getCategoryName())
                .orderByDesc(RoomCategory::getOrderNum);
        IPage<RoomCategory> list = roomCategoryService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }

    //分类下拉数据
    @GetMapping("/select")
    public ResultVo select(){
        List<RoomCategory> list = roomCategoryService.list();
        List<SelectMajor> selectMajors = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .stream()
                .forEach(item ->{
                    SelectMajor major = new SelectMajor();
                    major.setLabel(item.getCategoryName());
                    major.setValue(item.getCategoryId());
                    selectMajors.add(major);
                });
        return ResultUtils.success("查询成功",selectMajors);
    }


}

